import 'dart:ui';

import '../utils/color_utils.dart';
import 'base/base_effect.dart';
import 'effect_template.dart';

class Effect20 extends EditableAnimationEffect {
  @override
  List<Color> get defaultColors {
    return const [
      Color(0xFFFFFF00),
      Color(0xFFFF0000)
    ];
  }

  @override
  bool get enableAngle => false;

  @override
  bool get enableColorWidth => false;

  @override
  bool get enableDensity => false;

  @override
  bool get enableEditColor => false;

  @override
  bool get enableSpeed => true;

  @override
  RangeArgument get speedRange => RangeArgument(0, 9, 8);

  @override
  bool get isFixedColorCount => true;

  @override
  EffectType get effectTyp => EffectType.effect20;

  @override
  Future<List<List<List<Color>>>> getFrames({EffectOperation? operation}) {

    var setColorList = getColorList(effectOperation: operation).first;
    double sizeWidth = size.width;
    double sizeHeight = size.height;
    var colorList = [
      Color(0xFF000000),
      setColorList,
    ];

    ///彩色行数
    double colorfulCount = sizeHeight;

    ///黑色行数
    double topCount = sizeHeight;

    int totalFrame = (topCount + colorfulCount).toInt();

    List<double> stops = [
      0.0,
      colorfulCount * 1.0,
    ];
    //底部
    var setColorList2 = getColorList(effectOperation: operation);
    var colorList2 = [
      setColorList2[1],
      Color(0xFF000000),
    ];
    double height2 = sizeHeight;

    ///彩色行数
    double colorfulCount2 = height2;

    ///黑色行数
    double downCount2 = height2;

    int totalFrame2 = (colorfulCount2 + downCount2).toInt();

    List<double> stops2 = [
      0.0,
      colorfulCount2 * 1.0,
    ];

    ///先计算单列的颜色排布
    List<Color> singleColumn = [];
    for (int i = 0; i < totalFrame; i++) {
      if (i <= colorfulCount) {
        var gradientColor =
        ColorUtils.lerpGradient(colorList, stops, i.toDouble());
        singleColumn.add(gradientColor);
      } else {
        singleColumn.add(Color(0xFF000000));
      }
    }
    List<Color> singleColumn2 = [];
    for (int i = 0; i < totalFrame2; i++) {
      if (i <= colorfulCount2) {
        var gradientColor2 =
        ColorUtils.lerpGradient(colorList2, stops2, i.toDouble());
        singleColumn2.add(gradientColor2);
      } else {
        singleColumn2.add(Color(0xFF000000));
      }
    }
// int single = singleColumn.length;
//     print('sing$single');
    List<List<List<Color>>> frames = [];
    for (int i = 0; i < totalFrame; i++) {
      List<List<Color>> frame = [];
      for (int j = 0; j < sizeWidth; j++) {
        List<Color> columns = [];
        for (int z = 0; z < sizeHeight; z++) {
          Color columns1 = singleColumn[(z - i) % singleColumn.length];
          Color columns2 = singleColumn2[(i + z) % singleColumn.length];
          // if(height < height*0.5){
          Color columns3 = Color.fromRGBO(
            255 - ((255 - columns1.red) * (255 - columns2.red) ~/ 255),
            255 - ((255 - columns1.green) * (255 - columns2.green) ~/ 255),
            255 - ((255 - columns1.blue) * (255 - columns2.blue) ~/ 255),
            1,
          );
          columns.add(columns3);
        }
        frame.add(columns);
      }
      frames.add(frame);
    }
    return Future.value(frames);
  }

  @override
  Size get size => const Size(24, 24);
}